package com.kedou.sdk.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kedou.sdk.params.RedpackageResponseParams;
import com.kedou.sdk.params.bean.RedpackageItem;
import com.kedou.sdk.ui.view.CountdownTextView;
import com.kedou.sdk.ui.view.ListViewForScrollView;
import com.kedou.sdk.utils.ClickUtil;
import com.kedou.sdk.utils.ResUtils;

import java.util.List;

public class RedPackageFragment extends Fragment  implements View.OnClickListener {

//    private EditText mIdentificationNameEt;
//    private EditText mIdentificationCardEt;
//
//    private Button mBindPhoneBtn;


//    public static RedPackageFragment newInstance(Bundle bundle) {
//        RedPackageFragment fragment = new RedPackageFragment();
//        fragment.setArguments(bundle);
//        return fragment;
//    }
    private RedpackageResponseParams.RedpackageResponseData data ;

    private ChangeFragmentLister changeFragmentLister;
    private ListViewForScrollView tastListView;
//    private BaseAdapter mAdapter;
    private List<RedpackageItem> levelPackageList,rechargePackageList ,curretPackageList;

    private LinearLayout detailLayout,tixianLayout,redpackageLevelLayout,redpackageMoneyLayout;
    private TextView moneyText,balanceText,roleIdText,roleNameText,roleLevelText,roleTimeText;
    CountdownTextView balancetime;

    public RedPackageFragment(ChangeFragmentLister changeFragment){
        this.changeFragmentLister = changeFragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "hnssn_fragment_redpackage");
        View contentView = inflater.inflate(resource, null);
        setView(contentView);

        return contentView;
    }

    public void updateDate(RedpackageResponseParams.RedpackageResponseData date){
        this.data = date;
//        data = getArguments().getParcelable("date");
//        roleNameText.setText(data.getNickname());
//        roleIdText.setText("ID："+data.getRole_id());
//        roleLevelText.setText("等级："+data.getRole_level()+"级");
//        roleTimeText.setText("（"+data.getAdd_time_text()+"）");
//        moneyText.setText(data.getBalance());
//        balanceText.setText(data.getUnaccepted_hongbao());
//        levelPackageList = data.getLevel_hongbao();
//        rechargePackageList = data.getRecharge_hongbao();
//        curretPackageList = redpackageLevelLayout.isSelected()?levelPackageList:rechargePackageList;
////            Log.e("51wan",(curretPackageList==null?0:curretPackageList.size())+".."+(rechargePackageList==null?0:rechargePackageList.size()));
//
//        tastListView.setAdapter(getAdapter());
        onResume();
    }

    @Override
    public void onResume() {
        super.onResume();
        setData();
    }

    private void setData(){
//        if(getArguments().containsKey("date")){
//            data = getArguments().getParcelable("date");

        if(data==null&&getArguments().containsKey("date")){
            data = getArguments().getParcelable("date");
        }
        if(roleNameText==null)return;
            roleNameText.setText(data.getNickname());
            roleIdText.setText("ID："+data.getRole_id());
            roleLevelText.setText("等级："+data.getRole_level()+"级");
            roleTimeText.setText("（"+data.getAdd_time_text()+"）");
            moneyText.setText(data.getBalance());
            balanceText.setText(data.getUnaccepted_hongbao());
            if("0".equals(data.getHongbao_expiration_time())){
                balancetime.setText("活动已结束");
            }else{
                //todo
                balancetime.init("倒计时：%s",Integer.valueOf(data.getHongbao_expiration_time()));
                balancetime.start(0);
            }
            levelPackageList = data.getLevel_hongbao();
            rechargePackageList = data.getRecharge_hongbao();
            curretPackageList = redpackageLevelLayout.isSelected()?levelPackageList:rechargePackageList;
//            Log.e("51wan",(curretPackageList==null?0:curretPackageList.size())+".."+(rechargePackageList==null?0:rechargePackageList.size()));

            tastListView.setAdapter(getAdapter());
//        }
    }

    public BaseAdapter getAdapter(){
       return new BaseAdapter() {
            @Override
            public int getCount() {
                return curretPackageList==null?0:curretPackageList.size();
            }

            @Override
            public Object getItem(int position) {
                return curretPackageList.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final RedpackageItem currentPackage = curretPackageList.get(position);
                ViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder = new ViewHolder();
                    final Context context = parent.getContext();
                    LayoutInflater inflater = LayoutInflater.from(context);
                    final int layoutResId;
                    if(currentPackage.getHongbaoStatus().getStatus()<2){
                        layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_redpackage");
                    }else{
                        layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_redpackage_unable");
                    }
                    final int moneyResIds = ResUtils.getViewIdentifier(context, "redpackage_item_money");
                    final int contentResId = ResUtils.getViewIdentifier(context, "redpackage_item_content");
                    final int buttonResId = ResUtils.getViewIdentifier(context, "redpackage_item_button");
                    convertView = inflater.inflate(layoutResId, parent, false);
                    viewHolder.money = convertView.findViewById(moneyResIds);
                    viewHolder.content = convertView.findViewById(contentResId);
                    viewHolder.button = convertView.findViewById(buttonResId);

                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.money.setText(currentPackage.getMoney());
                viewHolder.content.setText(currentPackage.getTitle());
                viewHolder.button.setText(currentPackage.getHongbaoStatus().getBtn_text());
                viewHolder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(ClickUtil.isFastClick()&&currentPackage.getHongbaoStatus().getStatus()==1){
                            changeFragmentLister.getPakage(currentPackage.getId());
                        }
                    }
                });

                return convertView;
            }
        };
    }


    private void setView(final View view){
        detailLayout = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "repackage_detail_layout"));
        tixianLayout = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "repackage_detail_tixian"));
        detailLayout.setOnClickListener(this);
        tixianLayout.setOnClickListener(this);
        roleNameText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_name"));
        roleIdText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_id"));
        roleLevelText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_level"));
        roleTimeText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_time"));
        moneyText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_money"));
        balanceText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_balance"));
        balancetime = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "redpackage_banlance_time"));
        view.setOnClickListener(this);
        redpackageLevelLayout = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "repackage_list_level"));
        redpackageMoneyLayout = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "repackage_list_money"));
        redpackageLevelLayout.setSelected(true);
        redpackageLevelLayout.setOnClickListener(this);
        redpackageMoneyLayout.setOnClickListener(this);
        tastListView = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "repackage_task_list"));

//        tastListView.setAdapter(mAdapter);
    }


    public static class ViewHolder {
        public TextView money;
        public TextView content;
        public TextView button;
    }


    @Override
    public void onClick(View v) {
        if(v == detailLayout){
            changeFragmentLister.changeToDetail();
        }else if(v == tixianLayout){
            changeFragmentLister.changeToTixian();
        }else if(v == redpackageLevelLayout){
            if(!redpackageLevelLayout.isSelected()){
                redpackageLevelLayout.setSelected(true);
                redpackageMoneyLayout.setSelected(false);
                curretPackageList = levelPackageList;
//                mAdapter.notifyDataSetChanged();
                tastListView.setAdapter(getAdapter());
            }
        }else if (v == redpackageMoneyLayout){
            if(!redpackageMoneyLayout.isSelected()){
                redpackageLevelLayout.setSelected(false);
                redpackageMoneyLayout.setSelected(true);
                curretPackageList = rechargePackageList;
//                mAdapter.notifyDataSetChanged();
                tastListView.setAdapter(getAdapter());
            }
        }
    }
}
